package game;

import hibernateUtil.Playerinfo;
import io.ClientSession;
import io.OperationResponse;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import packethandler.CarbonHandler;
import player.Player;
import protocol.ParameterType;
import protocol.ReturnCode;
import MemoryDB.YuanTable;
import PlayerInfo.PlayerInfoCache;
import Public.PlayerPublicFun;
/**
 * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷呒锟�
 * @author zhengjiangyu
 *
 */
public class SendPack {
	
	protected static final Logger log = LoggerFactory.getLogger(SendPack.class);
    public void SendPacks(OperationResponse operationRequest,ClientSession session)
    {
    	
    	
        try
        {
            String itemID = (String)operationRequest.Parameters.get(ParameterType.ItemID);
            String userName = (String)operationRequest.Parameters.get(ParameterType.UserNickName);
            if (PlayerPublicFun.PubGameItemForItemID.containsKey(itemID.split("[,]",-1)[0]))
            {

                if (userName.equals("0") )
                {
                    for (int i = 0; i < PlayerInfoCache.SIZE(); i++)
                    {
                        GetPack(PlayerInfoCache.Find(i), itemID,session);
                    }
                }
                else
                {
                	Playerinfo playerinfo=null;
                	playerinfo = PlayerInfoCache.Find(userName);
                    if (playerinfo!=null)
                    {
                      GetPack(playerinfo, itemID,session);
                    }
                    else
                    {
                        OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.NoServer,"");
                         session.SendOperationResponse(response);
                        return;
                    }
                }
                OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.Yes,"");
                session.SendOperationResponse(response);


            }
            else
            {
            	   OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.No,"");
                   session.SendOperationResponse(response);

            }
        }
        catch (Exception ex)
        {
        	log.error(ex.fillInStackTrace().toString());
     	   OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.Error,ex.fillInStackTrace().toString());
           session.SendOperationResponse(response);
        }
    }
    public static void GetPack(Playerinfo tempTable,String itemID,ClientSession session)
    {
        HashMap<Short, Object> sendParem = new HashMap<Short,Object>();
        if (itemID.substring(0, 2).equals("88"))
        {
        	tempTable.setItem(String.format("%1$S%2$2S;", tempTable.getItem().toString(), itemID));

            if (PlayerPublicFun.onLineActors.containsKey(tempTable.getPlayerID()))
            {
                String[] strKey = new String[1];
                String[] strValue = new String[1];
                strKey[0] = "Item";
                strValue[0] = tempTable.getItem();
                sendParem.put(ParameterType.TableKey, strKey);
                sendParem.put(ParameterType.TableSql, strValue);
                PlayerPublicFun.RefershTableSome(sendParem,session);
            }
        }
        else
        {
        	tempTable.setGetPacks(String.format("%1$S%2$S;", tempTable.getGetPacks().toString(), itemID));

            if (PlayerPublicFun.onLineActors.containsKey(tempTable.getPlayerID()))
            {
                String[] strKey = new String[1];
                String[] strValue = new String[1];
                strKey[0] = "GetPacks";
                strValue[0] = tempTable.getGetPacks();
                sendParem.put(ParameterType.TableKey, strKey);
                sendParem.put(ParameterType.TableSql, strValue);
                PlayerPublicFun.RefershTableSome(sendParem,session);
            }
        }
    }
    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟接d锟叫憋拷锟斤拷锟�
     */
    public static void SendPacksForIDs(OperationResponse operationRequest,String[] listPlayerID,ClientSession session)
    {
        try
        {
            //int[] listPlayerID = (int[])operationRequest.Parameters[(byte)ParameterType.UserID];
            String itemID = (String)operationRequest.Parameters.get(ParameterType.ItemID);
            if (PlayerPublicFun.PubStoreItem.containsKey(itemID.split("[,]",-1)[0]))
            {
            	Playerinfo tempTable=null;
//                YuanServerPeer tempPeer = null;
//                YuanTable tempTable = null;
                HashMap<Short, Object> sendParem = new HashMap<Short, Object>();
                for (String tempID :listPlayerID)
                {
                	tempTable = PlayerInfoCache.Find(Integer.parseInt(tempID));
                    if (tempTable!=null)
                    {
                    	tempTable.setGetPacks(String.format("%1$s%2$s;", tempTable.getGetPacks(),itemID));

                        if (PlayerPublicFun.onLineActors.containsKey(tempTable.getPlayerID()))
                        {
                        	
                            String[] strKey = new String[1];
                            String[] strValue = new String[1];
                            strKey[0] = "GetPacks";
                            strValue[0] = tempTable.getGetPacks();
                            sendParem.put(ParameterType.TableKey, strKey);
                            sendParem.put(ParameterType.TableSql, strValue);
                            
                           
                            PlayerPublicFun.RefershTableSome(sendParem,session);
                        }
                    }
                }
                OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.Yes,"");
                session.SendOperationResponse(response);
            }
        }
        catch (Exception ex)
        {
        	log.error(""+ex);
        	OperationResponse response = new OperationResponse(operationRequest.OperationCode,null,ReturnCode.Error,""+ex);
            session.SendOperationResponse(response);
        }
    }
    /**
     * 锟斤拷取锟斤拷锟斤拷械锟窖拷锟斤拷
     */
    
   public static int[] GetCostMoneyInTablePacks(String lastStr)
    {
        int[] money = new int[4];
        money[0] = 0;
        money[1] = 0;
        money[2] = 0;
        money[3] = 0;
        
        YuanTable ytItem = null;
        if(PlayerPublicFun.PubGameItemForItemID.containsKey(lastStr.substring(0,4))){
        	ytItem=PlayerPublicFun.PubGameItemForItemID.get(lastStr.substring(0,4));
        	PlayerPublicFun.PubGameItemForItemID.put(lastStr.substring(0,4), ytItem);
        	if(PlayerPublicFun.pubPack.containsKey(ytItem.getItem(0).getItem("ItemValue").getYuanColumnText())){
        		ytItem=PlayerPublicFun.pubPack.get(ytItem.getItem(0).getItem("ItemValue").getYuanColumnText());
        		money[1]=(!ytItem.getItem(0).getItem("BloodStrone").getYuanColumnText().equals(""))?Integer.parseInt(ytItem.getItem(0).getItem("BloodStrone").getYuanColumnText()):0;
        		money[0]=(!ytItem.getItem(0).getItem("Cash").getYuanColumnText().equals(""))?Integer.parseInt(ytItem.getItem(0).getItem("Cash").getYuanColumnText()):0;
        		
        		money[2]=(!ytItem.getItem(0).getItem("FineIronPow").getYuanColumnText().equals(""))?Integer.parseInt(ytItem.getItem(0).getItem("FineIronPow").getYuanColumnText()):0;
        		money[3]=(!ytItem.getItem(0).getItem("GoldCrystal").getYuanColumnText().equals(""))?Integer.parseInt(ytItem.getItem(0).getItem("GoldCrystal").getYuanColumnText()):0;
        	}
        	
        }
        return money;
    }
  

}
